/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package consum.bean;

import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import java.io.Serializable;
import java.io.StringReader;
import java.lang.Long;
import java.lang.String;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;
import javax.ws.rs.core.MultivaluedMap;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import trial.entity.Customer;
import trial.util.customerSet;
import webservice.restful.client2.restfulClient2;
import java.util.List;
import javax.ws.rs.core.MediaType;
/**
 *
 * @author feifei
 */
@ManagedBean
@ViewScoped
public class UserManagedBean implements Serializable{

    /** Creates a new instance of userManagedBean */
    public UserManagedBean() {
    }
    private Long id;
    private String username;
    private String country;
    private String zipcode;
    private String email;
    private String password;
    private String bankaccount;
    List<Customer> allUsers;

    public List<Customer> getAllUsers() throws JAXBException{
      restfulClient2 client2 =new restfulClient2();
      String xml=client2.getAllCurrentCustomers(MediaType.APPLICATION_XML.getClass(),null);
         if(!xml.isEmpty())
         {
        StringReader stringReader = new StringReader(xml);
        JAXBContext jc = JAXBContext.newInstance(customerSet.class); 
        customerSet productSet = (customerSet)jc.createUnmarshaller().unmarshal(stringReader);
        return productSet.getCustomers();
         }
         return null;
    }
    public void setAllUsers(List<Customer> allUsers) {
        this.allUsers = allUsers;
    }
    
    public String getBankaccount() {
        return bankaccount;
    }

    public void setBankaccount(String bankaccount) {
        this.bankaccount = bankaccount;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getZipcode() {
        return zipcode;
    }

    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }
    public void deleteUser(ActionEvent event)
    {
        Long id=(Long) event.getComponent().getAttributes().get("ID");
        MultivaluedMapImpl formData = new MultivaluedMapImpl();
        formData.add("id",id);
        restfulClient2 client2 =new restfulClient2();
        String response=client2.deleteUser(String.class, formData);
        System.out.println(response);
      
    }
    public void submitUser(ActionEvent event) throws JAXBException
    {
         MultivaluedMap formData = new MultivaluedMapImpl();
         formData.add("username", this.username);
         formData.add("country",this.country);
         formData.add("zipcode", this.zipcode);
         formData.add("email",this.email);
         formData.add("password",this.password);
         formData.add("bankaccount",this.bankaccount);
         restfulClient2 client2 =new restfulClient2();
         String response=client2.createUser(String.class,formData);
         System.out.println("userManagedBean Test*********************"+response);
         client2.close();
         
         
      }
    
}
